package com.pan.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public enum TemplateReader {
    INSTANCE;
    private ClassLoader loader;{
        loader = TemplateReader.class.getClassLoader();
    }
    private Map<String,String> templatePools;{
        templatePools = new HashMap<>();
    }
    public static final String CELL = "template/Cell.xml";
    public static final String CLASS_NAME = "template/ClassName.xml";
    public static final String LINE = "template/Line.xml";
    public static final String PICTURE = "template/PictureTemplate.xml";


    private String readFile(String name){
        if (templatePools.containsKey(name)){
            return templatePools.get(name);
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(loader.getResourceAsStream(name)));
        String template = reader.lines().reduce((x,y)->x+"\n"+y).get();
        templatePools.put(name,template);
        return template;
    }
    public String readCell(){
        return readFile(CELL);
    }
    public String readClassName(){
        return readFile(CLASS_NAME);
    }
    public String readLine(){
        return readFile(LINE);
    }
    public String readPicture(){
        return readFile(PICTURE);
    }

}
